/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Description: supply cast dlna player manager.
 * Author: zhangjingnan
 * Create: 2023-1-11
 */

#ifndef DLNA_STREAM_PLAYER_MANAGER_H
#define DLNA_STREAM_PLAYER_MANAGER_H

#include "dlna_stream_player.h"
#include "i_dlna_player.h"
#include "i_dlna_player_listener.h"
#include "i_dlna_stream_manager_calback.h"
namespace OHOS {
namespace CastEngine {
namespace CastEngineService {
class DlnaStreamPlayerManager : public IDlnaPlayer,
    public std::enable_shared_from_this<DlnaStreamPlayerManager> {
public:
    DlnaStreamPlayerManager(std::shared_ptr<IDlnaStreamManagerCallback> callback);
    ~DlnaStreamPlayerManager();

    void SetSessionCallbackForRelease(const std::function<void(void)>& callback);
    int32_t RegisterListener(sptr<IDlnaPlayerListener> listener) ;
    int32_t UnregisterListener() ;
    int32_t SetSurface(sptr<IBufferProducer> producer) ;
	int32_t SetSurface_ext(sptr<Surface> surface);
    int32_t SetSurface_str(std::u16string& surfaceId);
    int32_t Load(const MediaInfo &mediaInfo) ;
    int32_t Prepare() ;
    int32_t Play(const MediaInfo &mediaInfo) ;
    int32_t Play(int index) ;
    int32_t Play() ;
    int32_t Pause() ;
    int32_t Stop() ;
    int32_t Next() ;
    int32_t Previous() ;
    int32_t Seek(int position) ;
    int32_t SetVolume(int volume) ;
    int32_t SetLoopMode(const LoopMode mode) ;
    int32_t SetSpeed(const PlaybackSpeed speed) ;
    int32_t GetPlayerStatus(PlayerStates &playerStates) ;
    int32_t GetPosition(int &position) ;
    int32_t GetDuration(int &duration) ;
    int32_t GetVolume(int &volume, int &maxVolume) ;
    int32_t GetPlaySpeed(PlaybackSpeed &playbackSpeed) ;
    int32_t Release() ;

private:
    bool InnerPlayLocked(bool isLoading);
    bool StopLocked();
    PlaybackSpeed ConvertMediaSpeedToPlaybackSpeed(Media::PlaybackRateMode speedMode);
    std::function<void(void)> sessionCallback_;

    std::mutex mutex_;
    std::mutex sessionCallbackMutex_;
    std::shared_ptr<DlnaStreamPlayer> player_;
    std::shared_ptr<DlnaStreamPlayerCallback> callback_;
    MediaInfo mediaInfo_{};
    std::atomic<bool> isReceiveLoadCommand_{ false };
    std::atomic<bool> isReceivePlayCommand_{ false };
    std::atomic<bool> isReady_{ false };
    sptr<Surface> surface_;
};
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS
#endif // DLNA_STREAM_PLAYER_MANAGER_H